package com.richard.base.net.utils

import android.annotation.SuppressLint
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkInfo
import android.telephony.TelephonyManager

/**
 ***************************************
 * 项目名称:Giggle
 * @Author wuzhiguo
 * 邮箱：wuzhiguo@ksjgs.com
 * 创建时间: 2020/11/4     5:26 PM
 * 用途:
 ***************************************
 */
object NetworkUtils {

    var NETWORK_STATE_CONNECTING = -2
    var NETWORK_STATE_NONE = -1
    var NETWORK_STATE_WIFI = 1
    var NETWORK_STATE_2G = 2
    var NETWORK_STATE_3G = 3
    var NETWORK_STATE_4G = 4
    var NETWORK_STATE_MOBILE_UNKNOWN = 5

    /**
     * get current network connected type
     *
     * @param context context
     * @return int
     */
    fun getNetworkState(context: Context): Int {
        val connManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
                ?: return NETWORK_STATE_NONE // 获取网络服务
        @SuppressLint("MissingPermission") val networkInfo =
            connManager.activeNetworkInfo
        if (networkInfo == null) {
            return NETWORK_STATE_NONE
        } else {
            val networkInfoState = networkInfo.state
            if (networkInfoState == NetworkInfo.State.CONNECTING) {
                return NETWORK_STATE_CONNECTING
            }
            if (!networkInfo.isAvailable) {
                return NETWORK_STATE_NONE
            }
        }
        // is wifi ?
        @SuppressLint("MissingPermission") val wifiInfo =
            connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        if (null != wifiInfo) {
            val state = wifiInfo.state
            if (null != state) {
                if (state == NetworkInfo.State.CONNECTED || state == NetworkInfo.State.CONNECTING) {
                    return NETWORK_STATE_WIFI
                }
            }
        }
        // 2G、3G、4G ?
        val telephonyManager =
            context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val networkType = telephonyManager.networkType
        return when (networkType) {
            TelephonyManager.NETWORK_TYPE_GPRS, TelephonyManager.NETWORK_TYPE_CDMA, TelephonyManager.NETWORK_TYPE_EDGE, TelephonyManager.NETWORK_TYPE_1xRTT, TelephonyManager.NETWORK_TYPE_IDEN -> NETWORK_STATE_2G
            TelephonyManager.NETWORK_TYPE_EVDO_A, TelephonyManager.NETWORK_TYPE_UMTS, TelephonyManager.NETWORK_TYPE_EVDO_0, TelephonyManager.NETWORK_TYPE_HSDPA, TelephonyManager.NETWORK_TYPE_HSUPA, TelephonyManager.NETWORK_TYPE_HSPA, TelephonyManager.NETWORK_TYPE_EVDO_B, TelephonyManager.NETWORK_TYPE_EHRPD, TelephonyManager.NETWORK_TYPE_HSPAP -> NETWORK_STATE_3G
            TelephonyManager.NETWORK_TYPE_LTE -> NETWORK_STATE_4G
            else -> NETWORK_STATE_MOBILE_UNKNOWN
        }
    }

    fun isMobile(networkState: Int): Boolean {
        return networkState > NETWORK_STATE_WIFI
    }

    /**
     * whether or not network connect.
     *
     * @param context context
     * @return true/false
     */
    fun isNetConnected(context: Context): Boolean {
        val connectivity =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (connectivity != null) {
            @SuppressLint("MissingPermission") val info =
                connectivity.activeNetworkInfo
            if (info != null && info.isConnected) {
                if (info.state == NetworkInfo.State.CONNECTED) {
                    return true
                }
            }
        }
        return false
    }

    /**
     * is wifi ?
     *
     * @param context context
     * @return true/false
     */
    @Synchronized
    fun isWifiConnected(context: Context): Boolean {
        val connectivityManager =
            context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        if (connectivityManager != null) {
            @SuppressLint("MissingPermission") val networkInfo =
                connectivityManager.activeNetworkInfo
            if (networkInfo != null) {
                val networkInfoType = networkInfo.type
                if (networkInfoType == ConnectivityManager.TYPE_WIFI || networkInfoType == ConnectivityManager.TYPE_ETHERNET) {
                    return networkInfo.isConnected
                }
            }
        }
        return false
    }
}